package com.huixin.zhang.service.core.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import com.github.pagehelper.PageInfo;
import com.huixin.zhang.core.page.PageRequest;
import com.huixin.zhang.core.page.PageResult;
import com.huixin.zhang.persistence.core.EntityCurdDao;
import com.huixin.zhang.service.core.EntityCurdService;
import com.github.pagehelper.PageHelper;

/***
 * BasicIDEntityServiceImpl
 * @author qiuyp
 * @version 1.0
 * @since 2020年6月24日 下午10:20:40
 */
public abstract class EntityCurdServiceImpl<T, D extends EntityCurdDao<T, ID>,ID extends Serializable> implements EntityCurdService<T, ID> {

	protected abstract D getBasicIDEntityDao();

	@Override
	public List<T> queryAll(T entity) {
		return getBasicIDEntityDao().queryAll(entity);
	}

	@Override
	public T queryById(ID id) {
		return getBasicIDEntityDao().queryById(id);
	}

	@Override
	public List<T> queryAllByLimit(int offset, int limit) {
		return getBasicIDEntityDao().queryAllByLimit(offset, limit);
	}

	@Override
	public List<T> selectPageList(Map<String, Object> map){
		return getBasicIDEntityDao().selectPageList(map);
	}

	@Override
	public int insert(T entity) {
		return getBasicIDEntityDao().insert(entity);
	}

	@Override
	public int update(T entity) {
		return getBasicIDEntityDao().update(entity);
	}

	@Override
	public boolean deleteById(ID id) {
		return getBasicIDEntityDao().deleteById(id) > 0;
	}

	@Override
	public PageResult<T> findPage(PageRequest pageRequest) {
		if(pageRequest.getPageNum()!=null && pageRequest.getPageSize()!=null)
			PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
		List<T> contents = getBasicIDEntityDao().selectPageList(pageRequest.getParams());
		PageInfo<T> page = new PageInfo<T>(contents);
		return PageResult.createPage(pageRequest, page);
	}
}
